package nip.com.swiping.activity;

import java.util.ArrayList;

import nip.com.swiping.adapter.ClassHolder.ViewHolder;
import nip.com.swiping.adapter.IconAdapter;
import nip.com.swiping.entity.IconEntity;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ListView;

import com.nip.swipetoremoveitemlistview.R;

public class SwippingAcitivy extends Activity implements OnTouchListener {
	private ListView _lvView;
	private OnTouchListener _onTouch;
	private ArrayList<IconEntity> _Sources;
	private IconAdapter _mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.swipping_activity);
		Initialize();
	}

	private void Initialize() {
		_lvView = (ListView) findViewById(R.id.listView1);
		_Sources = new ArrayList<IconEntity>();
		IconEntity icon = new IconEntity();
		icon.setIconName("AAAAAAAAAAAAA");
		icon.setIconSource(R.drawable.ic_launcher);
		_Sources.add(icon);
		icon = new IconEntity();
		icon.setIconName("BBBBBBBBBBBBB");
		icon.setIconSource(R.drawable.ic_launcher);
		_Sources.add(icon);
		icon = new IconEntity();
		icon.setIconName("CCCCCCCCCCCCC");
		icon.setIconSource(R.drawable.ic_launcher);
		_Sources.add(icon);
		icon = new IconEntity();
		icon.setIconName("DDDDDDDDDDDDD");
		icon.setIconSource(R.drawable.ic_launcher);
		_Sources.add(icon);
		_mAdapter = new IconAdapter(SwippingAcitivy.this, _Sources, this);
		_lvView.setAdapter(_mAdapter);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		int padding = 0;
		int initializeX = 0;
		int currentX = 0;
		ViewHolder viewHolder = null;

		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			padding = 0;
			initializeX = (int) event.getX();
			currentX = (int) event.getX();
			viewHolder = (ViewHolder) v.getTag();
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			currentX = (int) event.getX();
			padding = currentX - initializeX;
		}

		if (event.getAction() == MotionEvent.ACTION_CANCEL
				|| event.getAction() == MotionEvent.ACTION_UP) {
			padding = 0;
			initializeX = 0;
			currentX = 0;
		}
		
		if(viewHolder != null)
		{
			v.setPadding(padding, 0, 0, 0);
		}
		return false;
	}
}
